#include <bits/stdc++.h>
using namespace std;
constexpr int N = 6e5 + 5;
int n, vis[N];
vector<int> g[N];
signed main() {
    // freopen("//?/i:/desktop/slv/cqyc-/pairint5.in", "r", stdin);
    freopen("pairint.in", "r", stdin);
    freopen("pairint.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    int res = 0;
    for (int i = 1; i <= n; ++i) {
        int u, v;
        cin >> u >> v;
        u += n, v += n;
        g[i].push_back(u);
        g[i].push_back(v);
        g[u].push_back(i);
        g[v].push_back(i);
    }
    for (int i = 1; i <= n; ++i)
        if (!vis[i]) {
            queue<int> q;
            q.push(i);
            vis[i] = 1;
            int c = 0, cn = 0;
            while (!q.empty()) {
                int u = q.front();
                q.pop();
                if (u <= n)
                    ++c;
                else
                    ++cn;
                for (auto i : g[u])
                    if (!vis[i]) {
                        vis[i] = 1;
                        q.push(i);
                    }
            }
            res += min(c, cn);
        }
    cout << res;
    return 0;
}
